package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;

import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addShoppingCard(ShoppingCartDTO shoppingCartDTO) {
        //用户点击add

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId()); //设置用户id
        //按条件搜索 看购物车里面是否有数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //其实搜索出来的数据只有一条 相同userid dishid 口味 不可能在购物车里面存在多条
        if (shoppingCartList != null && shoppingCartList.size()>0 ) { //原来购物车中有数据 现在用户点击add 那么数量加1 更新操作
            ShoppingCart cart = shoppingCartList.get(0); //查询得到完整信息
            cart.setNumber(cart.getNumber() + 1); //数量更新
            log.info("执行数量+1操作");
            shoppingCartMapper.updateNumberById(cart);
        }
        else{//如果购物车中还没有这个数据 那么就添加

//当前shoppingCart具有了 userId 和前端传来的 dishId setmelId  dishflavor数据
            //我们需要将数据补充完整

            //如果是菜品 就根据dishid查询菜品表 得到数据
            if(shoppingCartDTO.getDishId()!=null){
                Dish dish=dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

            }   //如果是套餐 就根据setmealid查询套餐表setmeal 得到数据
            else if(shoppingCartDTO.getSetmealId()!=null){
                Setmeal setmeal=setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            //插入购物车数据
            shoppingCartMapper.insert(shoppingCart);

           }

        }


    @Override
    public List<ShoppingCart> showShoppingCard() {
        ShoppingCart shoppingCart =new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void cleanShoppingCard() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCard(ShoppingCartDTO shoppingCartDTO) {
//         class ShoppingCartDTO
//            private Long dishId;
//            private Long setmealId;
//            private String dishFlavor;

        //数据表中必然有这个数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);//数据拷贝到shoppingCart
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //搜索出这个数据 根据userId dishId setmealId dishFlavor
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //数量>1 直接执行更新数据的操作就可
        if (shoppingCartList != null && shoppingCartList.size() > 0) { //这是必然的结果 也可以不写


            ShoppingCart cart= shoppingCartList.get(0); //数据表中的数据
            if(cart.getNumber()>1){ //数量--
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(cart);
            }
            else{
                //原来数量就只有1个 在数据表中直接删除该商品即可
                // 根据userId dishId setmealId dishFlavor dishId(setmealId) 动态删除
                shoppingCartMapper.deleteShoppingCartItem(cart);
            }



        }




    }







//
//    @Override
//    public void subShoppingCard(ShoppingCartDTO shoppingCartDTO) {
//        ShoppingCart shoppingCart = new ShoppingCart();
//        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
//        shoppingCart.setUserId(BaseContext.getCurrentId());
//        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
//        if (shoppingCartList != null && shoppingCartList.size() == 1) {
//            shoppingCart = shoppingCartList.get(0);
//            if (shoppingCart.getNumber() > 1) {
//                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
//                shoppingCartMapper.updateNumberById(shoppingCart);
//            }else {
//                shoppingCartMapper.deleteById(shoppingCart.getId());
//            }
//        }
//    }

}
